package com.xuexiang.xuidemo.slice.status;

import com.xuexiang.xui_lib.component.AnimatorLayout;
import com.xuexiang.xui_lib.component.status.StatusLoader;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.AnimatorFactory;
import com.xuexiang.xuidemo.util.ShapeElementUtil;
import com.xuexiang.xuidemo.util.ToastUtil;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public abstract class BaseStatusLoaderSlice extends BaseAbilitySlice implements AnimatorLayout.OnAnimatorChangeListener, AnimatorLayout.OnMenuClickListener {

    private EventHandler mLoadingHandler = new EventHandler(EventRunner.getMainEventRunner());
    protected StatusLoader.Holder mHolder;

    private AnimatorLayout mAnimatorLayout;
    private AnimatorProperty imageAnimatorOut;
    private AnimatorProperty imageAnimator;
    private Image mRoundBtn;

    @Override
    protected void initComponents() {
        super.initComponents();
        setRound();
        mRoundBtn = (Image) findComponentById(ResourceTable.Id_round_image);
        mRoundBtn.setBackground(ShapeElementUtil.createRound(25, new RgbColor(42, 156, 220)));
        imageAnimator = AnimatorFactory.createRotate(mRoundBtn, 45);
        imageAnimatorOut = AnimatorFactory.createRotate(mRoundBtn, 0);

        mAnimatorLayout = (AnimatorLayout) findComponentById(ResourceTable.Id_animator_layout);
        mAnimatorLayout.setOnDismissListener(this);
        mAnimatorLayout.setMenuClickListener(this);

        mRoundBtn.setClickedListener(component -> {
            if (mAnimatorLayout.isOpen()) {
                mAnimatorLayout.dismissAllChild();
            } else {
                mAnimatorLayout.showAllChild();
            }
        });
    }

    @Override
    protected void onStop() {
        mLoadingHandler.removeAllEvent();
        super.onStop();
    }

    protected void initLoadingStatusViewIfNeed() {
        if (mHolder == null) {
            //此处设置需要包裹的布局
            mHolder = StatusLoader.from(getStatusLoaderAdapter())
                .wrap(getWrapView())
                .withRetry(this::onLoadRetry);
        }
    }

    /**
     * 重试
     * @param view
     */
    protected void onLoadRetry(Component view) {
        ToastUtil.showToast(this, "点击重试");
        showLoading();
    }

    /**
     * 显示加载页面
     */
    protected void showLoading() {
        initLoadingStatusViewIfNeed();
        // 模拟加载
        if (mHolder.getCurState() != StatusLoader.STATUS_LOADING) {
            mLoadingHandler.postTask(() -> {
                if (mHolder.getCurState() == StatusLoader.STATUS_LOADING) {
                    showContent();
                }
            }, 3000);
        }
        mHolder.showLoading();
    }

    /**
     * 显示内容
     */
    protected void showContent() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadSuccess();
    }

    /**
     * 显示出错页面
     */
    protected void showError() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadFailed();
    }

    /**
     * 显示空页面
     */
    protected void showEmpty() {
        initLoadingStatusViewIfNeed();
        mHolder.showEmpty();
    }

    /**
     * 显示自定义布局页面
     */
    protected void showCustom() {
        initLoadingStatusViewIfNeed();
        mHolder.showCustom();
    }

    /**
     * 获取被包裹的内容组件
     *
     * @return 被包裹的内容组件
     */
    protected abstract Component getWrapView();

    /**
     * 获取状态加载适配器
     *
     * @return 状态加载适配器
     */
    protected abstract StatusLoader.Adapter getStatusLoaderAdapter();


    private void setRound() {
        ShapeElement round = ShapeElementUtil.createRound(25, new RgbColor(42, 156, 220));
        findComponentById(ResourceTable.Id_tag_1).setBackground(round);
        findComponentById(ResourceTable.Id_tag_2).setBackground(round);
        findComponentById(ResourceTable.Id_tag_3).setBackground(round);
        findComponentById(ResourceTable.Id_tag_4).setBackground(round);
        findComponentById(ResourceTable.Id_tag_5).setBackground(round);
    }

    @Override
    public void onStartAnimator() {
        imageAnimator.start();
    }

    @Override
    public void onDismissAnimator() {
        imageAnimatorOut.start();
    }

    @Override
    public void onClickMenu(Component component) {

    }
}
